import {defineStore} from "pinia"
import { getToken, removeToken, setToken } from '../utils/token-utils';
import { loginApi, logoutApi, getUserInfoApi } from '../api/login';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */

export const useUserInfoStore = defineStore('userInfo',{
    state:()=>({
        token: getToken() as string,
        name:''
    }),
    actions:{
       /**
     * 登陆
     * @param username 
     * @param password 
     */  

   async login(username:string,password:string){
    const {token} =await loginApi(username,password);
    this.token = token
    setToken(token)  
},
// 获取用户信息
async getInfo(){
    const info=await getUserInfoApi()
    console.log('info' ,info);
    
},
//重置/退出登录
async reset(){
    //  // 如果当前是登陆的, 请求退出登陆
    if (this.name) {
        await logoutApi()
      }
      // 删除local中保存的token
      removeToken()
}
    }
})